package com.atguigu.gmall.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SeckillRabbitConfig {

    /**
     * 创建秒杀的交换机
     * @return
     */
    @Bean("seckillExchange")
    public Exchange seckillExchange(){
        return ExchangeBuilder.topicExchange("seckill_exchange").build();
    }
    /**
     * 创建秒杀正常的队列
     * @return
     */
    @Bean("seckillNormalQueue")
    public Queue seckillNormalQueue(){
        return QueueBuilder.durable("seckill_normal_queue").build();
    }
    /**
     * 创建秒杀死信交换机
     * @return
     */
    @Bean("seckillDeadExchange")
    public Exchange seckillDeadExchange(){
        return ExchangeBuilder.topicExchange("seckill_dead_exchange").build();
    }

    /**
     * 创建秒杀的队列
     * @return
     */
    @Bean("seckillQueue")
    public Queue seckillQueue(){
        return QueueBuilder.durable("seckill_queue").build();
    }

    @Bean
    public Binding seckillBinding(@Qualifier("seckillExchange")Exchange seckillNormalQueue,
                                  @Qualifier("seckillQueue")Queue seckillQueue){
        return BindingBuilder.bind(seckillQueue).to(seckillNormalQueue).with("seckill.#").noargs();
    }
    /**
     * 定义绑定:死信交换机和死信队列绑定
     */
    @Bean
    public Binding deadBinding(@Qualifier("seckillQueue") Queue seckillQueue,
                               @Qualifier("seckillDeadExchange") Exchange seckillDeadExchange){
        return BindingBuilder.bind(seckillQueue).to(seckillDeadExchange).with("seckill.dead.order").noargs();
    }
}
